package com.dykj.fireFighting.server.fire.contractNotifyRecord.service.impl;

import com.dykj.fireFighting.api.fire.contractMessage.entity.ContractMessage;
import com.dykj.fireFighting.api.fire.contractNotifyRecord.dto.ContractNotifyRecordPushDto;
import com.dykj.fireFighting.api.fire.contractNotifyRecord.entity.ContractNotifyRecord;
import com.dykj.fireFighting.api.fire.contractNotifyRecord.service.IContractNotifyRecordService;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.server.fire.contractNotifyRecord.cron.ContractTimmerComponent;
import com.dykj.fireFighting.server.fire.contractNotifyRecord.mapper.ContractNotifyRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @Author JacksonTu
 * @Date  2021-09-01 15:20:52
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class ContractNotifyRecordServiceImpl extends BaseServiceImpl<ContractNotifyRecordMapper, ContractNotifyRecord> implements IContractNotifyRecordService {

    @Autowired
    private ContractTimmerComponent contractTimmerComponent;

    @Override
    public boolean addContractNotify(ContractMessage contractMessage) {
        try {
            //清除所有的记录
            this.baseMapper.deleteByContractId(contractMessage.getId());
            List<ContractNotifyRecord> list = this.baseMapper.getByContractId(contractMessage.getId());
            for (ContractNotifyRecord record:list)
            {
                contractTimmerComponent.pauseeTask(record.getId());
            }

            //合同到期提醒

            if(contractMessage.getExpireWarnDay()!=null)
            {
                //添加到notify
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
                Date exDate=simpleDateFormat.parse(simpleDateFormat.format(contractMessage.getExpirationDate())+" 10:00:00");
                Date resultDate= DateUtils.addDays(exDate,0-contractMessage.getExpireWarnDay());
                ContractNotifyRecord contractNotifyRecord=new ContractNotifyRecord();
                contractNotifyRecord.setId(UUIDUtils.get32UUID());
                contractNotifyRecord.setContractId(contractMessage.getId());
                contractNotifyRecord.setCreateTime(new Date());
                contractNotifyRecord.setType(1);
                contractNotifyRecord.setAlias(contractMessage.getWeibaoUnitsCode());
                contractNotifyRecord.setNoticeTime(resultDate);
                this.baseMapper.insert(contractNotifyRecord);

                ContractNotifyRecordPushDto pushDto=new ContractNotifyRecordPushDto();
                BeanUtils.copyProperties(pushDto,contractNotifyRecord);
                pushDto.setTitle("合同到期通知");
                pushDto.setBody("维保合同号:"+contractMessage.getContractNumber()+"的合同即将到期，请注意查看");
                pushDto.setClickType("none");
                contractTimmerComponent.addTask(pushDto);


            }
            if(StringUtils.isNotBlank(contractMessage.getStagesWarnDate()))
            {
                String[] ss=contractMessage.getStagesWarnDate().split(",");
                for (String s:ss)
                {
                    ContractNotifyRecord contractNotifyRecord=new ContractNotifyRecord();
                    contractNotifyRecord.setId(UUIDUtils.get32UUID());
                    contractNotifyRecord.setContractId(contractMessage.getId());
                    contractNotifyRecord.setCreateTime(new Date());
                    contractNotifyRecord.setType(2);
                    contractNotifyRecord.setAlias(contractMessage.getWeibaoUnitsCode());
                    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    contractNotifyRecord.setNoticeTime(simpleDateFormat.parse(s+" 10:00:00"));
                    this.baseMapper.insert(contractNotifyRecord);


                    ContractNotifyRecordPushDto pushDto=new ContractNotifyRecordPushDto();
                    BeanUtils.copyProperties(pushDto,contractNotifyRecord);

                    pushDto.setTitle("付款提醒通知");
                    pushDto.setBody("维保合同号:"+contractMessage.getContractNumber()+"的合同需要付款了，请注意查看");

                    pushDto.setClickType("none");
                    contractTimmerComponent.addTask(pushDto);
                }
            }

        }catch (Exception e)
        {
            e.printStackTrace();
        }


        return true;
    }

    @Override
    public List<ContractNotifyRecord> queryUnDoneList() {
        return this.baseMapper.queryUnDoneList();
    }

}
